package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl  implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //添加套餐数据记录
        setmealMapper.save(setmeal);

        //添加套餐菜品数据记录
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            //获得套餐自增id,并为每条菜品记录设置相关套餐id
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.save(setmealDishes);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //分页插件pageHelper
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id获取套餐数据
     * @param id
     * @return
     */
    public SetmealVO getSetmealWithDish(Long id) {
        //查询套餐表数据
        SetmealVO setmealVO = setmealMapper.getById(id);

        //查询套餐表对应的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐表数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //删除对应套餐菜品表数据
        setmealDishMapper.delete(setmealDTO.getId());
        //添加对应套餐菜品表数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        setmealDishMapper.save(setmealDishes);
    }

    /**
     * 删除套餐及套餐相关菜品
     * @param ids
     */
    @Transactional
    public void deleteSetmealWithDish(List<Long> ids) {
        //起售中的套餐不能删除
        ids.forEach(id->{
            if(setmealMapper.getById(id).getStatus() == StatusConstant.ENABLE){
                //抛出错误异常
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(id->{
            //删除套餐表相关数据
            setmealMapper.delete(id);

            //删除对应菜品表数据
            setmealDishMapper.delete(id);
        });
    }

    /**
     * 套餐起售、停售
     * @param status
     */
    public void changeStatus(Integer status,Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param setmealId
     * @return
     */
    public List<DishItemVO> getDishById(Long setmealId) {
        List<DishItemVO> dishItemVO = setmealMapper.getDishBySetmealId(setmealId);
        return dishItemVO;
    }
}
